package cn.iocoder.yudao.module.resource.service.audiomark;

import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.resource.controller.admin.audio.vo.LmAudioRespVO;
import cn.iocoder.yudao.module.resource.controller.admin.keyword.vo.KeyWordRespVO;
import cn.iocoder.yudao.module.resource.controller.admin.tag.vo.TagRespVO;
import cn.iocoder.yudao.module.resource.dal.dataobject.audiomark.AudioMarkDO;
import cn.iocoder.yudao.module.resource.dal.dataobject.keyword.KeyWordDO;
import cn.iocoder.yudao.module.resource.dal.dataobject.tag.TagDO;
import cn.iocoder.yudao.module.resource.dal.mysql.audiomark.AudioMarkMapper;
import cn.iocoder.yudao.module.resource.service.keyword.KeyWordService;
import cn.iocoder.yudao.module.resource.service.tag.TagService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.List;

/**
 * 音频和标签及关键字的绑定 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class AudioMarkServiceImpl implements AudioMarkService {

    @Resource
    private AudioMarkMapper audioMarkMapper;
    @Resource
    private TagService tagService;
    @Resource
    private KeyWordService keyWordService;

    @Override
    public void updateAudioMark(Integer audioId, List<Integer> tagIds, List<Integer> keywordIds) {
        // 先删除旧有的绑定关系
        audioMarkMapper.removeOldMark(audioId);
        List<AudioMarkDO> audioMarkDOList = new ArrayList<>();
        // 再绑定新的关系
        for (int i = 0; i < 1000; i++) {
            AudioMarkDO audioMarkDO = new AudioMarkDO();
            audioMarkDO.setAudioId(audioId);
            if (tagIds != null && i < tagIds.size()) {
                Integer tagId = tagIds.get(i);
                audioMarkDO.setTagId(tagId);
            }
            if (keywordIds != null && i < keywordIds.size()) {
                Integer keywordId = keywordIds.get(i);
                audioMarkDO.setKeyWordId(keywordId);
            }
            if (audioMarkDO.getTagId() == null && audioMarkDO.getKeyWordId() == null) {
                break;
            } else {
                audioMarkDOList.add(audioMarkDO);
            }
        }
        audioMarkMapper.insertBatch(audioMarkDOList);
    }

    @Override
    public boolean isHaveMarkAudioWithTagId(Integer tagId) {
        List<AudioMarkDO> audioMarkDOList = this.audioMarkMapper.selectList(AudioMarkDO::getTagId, tagId);
        return audioMarkDOList.size() > 0;
    }


    @Override
    public boolean isHaveMarkAudioWithKeyword(Integer keywordId) {
        List<AudioMarkDO> audioMarkDOList = this.audioMarkMapper.selectList(AudioMarkDO::getKeyWordId, keywordId);
        return audioMarkDOList.size() > 0;
    }

    @Override
    public List<Integer> getAudioIdListByTag(List<String> tagList) {
        List<Integer> integers = this.audioMarkMapper.queryAudioIdListByTag(tagList);
        return integers;
    }

    @Override
    public List<Integer> getAudioIdListByKeyword(List<String> keywordList) {
        return this.audioMarkMapper.queryAudioIdListByKeyword(keywordList);
    }

    @Override
    public List<AudioMarkDO> getByAudioMarkIdList(List<Integer> audioIdList) {
        return this.audioMarkMapper.selectList(AudioMarkDO::getAudioId, audioIdList);
    }

    @Override
    public void deleteByAudioId(Integer audioId) {
        this.audioMarkMapper.delete(AudioMarkDO::getAudioId, audioId);
    }

    @Override
    public void deleteByAudioIds(List<Integer> audioId) {
        if (audioId.isEmpty()) {
            return;
        }
        LambdaQueryWrapper<AudioMarkDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(AudioMarkDO::getAudioId, audioId);
        this.audioMarkMapper.delete(queryWrapper);
    }

    @Override
    public void setTagAndKeyWord(List<LmAudioRespVO> audioRespVOS) {
        for (LmAudioRespVO vo : audioRespVOS) {
            List<AudioMarkDO> audioMarkDOList = audioMarkMapper.getAudioMarkByFileId(vo.getId());
            List<Integer> tagIdList = new ArrayList<>();
            List<Integer> keywordIdList = new ArrayList<>();
            for (AudioMarkDO audioMarkDO : audioMarkDOList) {
                if (audioMarkDO.getKeyWordId() != null) {
                    keywordIdList.add(audioMarkDO.getKeyWordId());
                }
                if (audioMarkDO.getTagId() != null) {
                    tagIdList.add(audioMarkDO.getTagId());
                }
            }
            if (tagIdList.size() > 0) {
                List<TagDO> tagDOList = tagService.getTagList(tagIdList);
                List<TagRespVO> tagNameList = BeanUtils.toBean(tagDOList, TagRespVO.class);
                vo.setTagIdArray(tagNameList);
            }
            if (keywordIdList.size() > 0) {
                List<KeyWordDO> keyWordDOList = keyWordService.getKeyWordList(keywordIdList);
                List<KeyWordRespVO> keywordList = BeanUtils.toBean(keyWordDOList, KeyWordRespVO.class);
                vo.setKeyWordIdArray(keywordList);
            }
        }
    }

}